package com.lcf;

import com.lcf.service.UserService;
import com.lcf.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Administrator on 2019/4/24.
 */
public class TestInvokeAop implements InvocationHandler {
    private Object target;//这是业务实现对象，用来调用具体的方法

    public TestInvokeAop(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result=null;

        System.out.println("动态代理---》插入数据前");
        result = method.invoke(target, args);
        System.out.println("动态代理---》插入数据后");
        return result;
    }

    public static void main(String[] args) {
        UserService userService = new UserServiceImpl();
        TestInvokeAop testInvokeAop = new TestInvokeAop(userService);

        ClassLoader classLoader = userService.getClass().getClassLoader();
        Class<?>[] interfaces = userService.getClass().getInterfaces();

        UserService newProxyInstance = (UserService) Proxy.newProxyInstance(classLoader, interfaces, testInvokeAop);

        newProxyInstance.add();
    }
}
